#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
	int fd = open("a.txt",O_CREAT | O_RDWR , 0666);
	if(fd == -1) perror("open") , exit(-1);

	struct flock lock;
	lock.l_type = F_WRLCK;
	lock.l_whence = SEEK_SET;
	lock.l_start = 0;
	lock.l_len = 10 ;
	lock.l_pid = -1;
	int res = fcntl(fd , F_GETLK , &lock);
	printf("F_RDLCK=%d, F_WRLCK=%d, F_UNLCK=%d\n",
		F_RDLCK,F_WRLCK,F_UNLCK);
	printf("type=%d, whence=%d, start=%d, len=%d, pid=%d\n",
		lock.l_type,lock.l_whence,lock.l_start,lock.l_len,lock.l_pid		);
	if(lock.l_pid == -1)
		printf("the file is not locking\n");
	else
	{
		printf("the file is locking , the pid = %d\n",lock.l_pid);		
	}
}
